package com.example.springboot.service;

import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public int save(User user){
        if(user.getId()==null){
            return userMapper.insert(user);
        }else{
            return userMapper.update(user);
        }
    }

    //批量增加
    public Integer saveBatch(List<User> users){
        Integer num = 0;
        for(int i=0; i<users.size();i++){
            User user = users.get(i);
            if(user.getId()==null){
                num += userMapper.insert(user);
            }else{
                num += userMapper.update(user);
            }
        }
        return num;
    }
}
